package com.swufe.myapplication;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import java.util.List;

public class MainActivity2 extends AppCompatActivity {
    private static final String TAG = "main2";
    private int score = 0;
    private TextView tvScore;
    private TextView topScore;
    private int currentTopScore;
    private static MainActivity2 mainActivity2;
    private SharedPreferences sp;
    private Button btnmusic;
    private Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(TAG, "onCreate: 2022/5/29第一次做");
        Log.i(TAG, "onCreate: 2022/6/2修改topscore存储");
        Log.i(TAG, "onCreate: 2022/6/8修改音效");
        setContentView(R.layout.activity_main2);
        initView();
        //读取最高分
        sp = getSharedPreferences("topscore1", Activity.MODE_PRIVATE);
        currentTopScore = sp.getInt("topScore1", 0);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onStop();
                Log.i(TAG, "onClick: 背景音停止");
            }
        });
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btnrestart://重新开始
                AlertDialog.Builder alert = new AlertDialog.Builder(this);
                alert.setTitle("提示：");
                alert.setMessage("你确定要重新开始吗？");
                alert.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        newGame();
                        Log.i(TAG, "onClick: 重新开始");
                    }
                });
                alert.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                alert.show();
                break;
            default:
                break;
            case R.id.btnquit://退出游戏
                AlertDialog.Builder dialog = new AlertDialog.Builder(this);
                dialog.setTitle("提示：");
                dialog.setMessage("你确定要离开吗？");
                dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        System.exit(0);
                        Log.i(TAG, "onClick: 退出");

                    }
                });
                dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                dialog.show();
                break;
            case R.id.btnhelp://帮助
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("亲爱的玩家");
                builder.setMessage("通过左右上下滑动，每次控制所有方块向同一个方向运动，两个相同数字的方块撞在一起之后合并成为他们的和，每次操作之后会在空白的方格处随机生成一个2或者4。如果16个格子全部填满并且相邻的格子都不相同也就是无法移动的话，那么很遗憾，Gameover。");
                builder.setNegativeButton("继续游戏", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Log.i(TAG, "onClick: 帮助");
                    }
                });
                builder.show();
                break;
            case R.id.btnmusic:
                Intent intent = new Intent(MainActivity2.this, MusicServer.class);
                startService(intent);
                Log.i(TAG, "onClick: 播放背景音");
        }
    }

    private void initView() {
        tvScore = (TextView) findViewById(R.id.tv_currentscore);
        topScore = (TextView) findViewById(R.id.tv_topscore);
        btnmusic = findViewById(R.id.btnmusic);
        btn =  findViewById(R.id.btn);
    }

    public MainActivity2() {
        mainActivity2 = this;
    }

    public static MainActivity2 getMainActivity() {
        return mainActivity2;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    //使分数显示出来
    public void showScore() {
        tvScore.setText(score + " ");
        topScore.setText(currentTopScore + " ");
        Log.i(TAG, "showScore: ");
    }

    //分数归零
    public void clearScore() {
        score = 0;
        showScore();
        Log.i(TAG, "clearScore: ");
    }

    //添加分数的方法
    public void addScore(int s) {
        score += s;
        showScore();
        Log.i(TAG, "addScore: ");
        if (score > currentTopScore) {
            currentTopScore = score;
            TopScore ts = new TopScore(this);
            ts.setTopScore(currentTopScore);
            topScore.setText("" + currentTopScore);
            //存储最高分
            sp = getSharedPreferences("topscore1", Activity.MODE_PRIVATE);
            SharedPreferences.Editor editor = sp.edit();
            editor.putInt("topScore1", currentTopScore);
            editor.apply();
        } else {
            TopScore top = new TopScore(this);
            top.getTopScore();
            topScore.setText("" + currentTopScore);
        }
    }

    //重置游戏
    public void newGame() {
        GameView2.gameView.startGame();
    }

    protected void onStop() {
        Intent intent = new Intent(MainActivity2.this, MusicServer.class);
        stopService(intent);
        super.onStop();
        Log.i(TAG, "onStop: 停");
    }
}

